package com.bamboo.tool.template.yapi.view.dialog;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.bamboo.tool.config.BambooToolComponent;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.entity.*;
import com.bamboo.tool.factory.FrameworkExecute;
import com.bamboo.tool.template.yapi.entity.*;
import com.bamboo.tool.template.yapi.util.YapiRequestUtil;
import com.bamboo.tool.util.DescProcessUtil;
import com.bamboo.tool.util.TaskUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

public class UploadYapiDialogWrapper extends DialogWrapper {
    private JPanel mainJPanel;
    private JComboBox projectBox;
    private JComboBox groupBox;
    private QueryClassParameter queryClassParam;

    private Project myProject;
    private List<String> descSorts;

    public UploadYapiDialogWrapper(QueryClassParameter queryClassParam, Project myProject) {
        super(false);
        init();
        YapiSetting setting = RestfulCloudApplicationCache.getInstance().getState().getYapiSetting();
        List<YapiGroup> groups = YapiRequestUtil.getGroups(setting);
        descSorts = FrameworkExecute.getAllDescDort();
        this.queryClassParam = queryClassParam;
        this.myProject = myProject;
        groupBox.removeAll();
        groups.forEach(groupBox::addItem);

        groupBox.addItemListener(e -> {
            if (e.getStateChange() == 1) {
                refreshProject(setting);

            }
        });
        refreshProject(setting);
    }

    @Override
    public @Nullable JComponent createCenterPanel() {
        return mainJPanel;
    }

    @Override
    protected void doOKAction() {
        TaskUtil.run(myProject, "Yapi 上传", () -> {
            YapiSetting setting = RestfulCloudApplicationCache.getInstance().getState().getYapiSetting();

            Object selectedItem = projectBox.getSelectedItem();
            if (selectedItem == null) {
                throw new RuntimeException("请选择项目");
            }
            YapiProject project = (YapiProject) selectedItem;
            String token = YapiRequestUtil.getToken(setting, project.getId());
            Collection<ApiClass> apiClass = BambooToolComponent.getStoreService().getApiClass(queryClassParam);

            List<YapiMenu> projectMenus = YapiRequestUtil.getProjectMenus(setting, token, project.getId());
            Map<String, YapiMenu> menuMap = projectMenus.stream().collect(Collectors.toMap(e -> e.getName(), e -> e, (key1, key2) -> key1));
            apiClass.forEach(e -> {
                String categoryName = "";
                ClassPersistenceInfo persistence = e.getPersistence();
                String rename = persistence.getRename();
                if (StrUtil.isNotBlank(rename)) {
                    categoryName = rename;
                } else {
                    String classDesc = DescProcessUtil.getDesc(descSorts, e.getDesc());
                    categoryName = StrUtil.isNotBlank(classDesc) ? classDesc : e.getClassName();
                }
                YapiMenu yapiMenu = menuMap.get(categoryName);
                if (yapiMenu == null) {

                } else {

                }
                e.getMethods().forEach(method -> {
                    String requestMethod = StrUtil.join(",", method.getRequestMethods());
                    yapiMenu.getList().stream().filter(a -> a.getPath().equals(method.getMethodUrl())).filter(a -> a.getMethod().equals(requestMethod));
                    YapiInterface yapiInterface = new YapiInterface();
                    yapiInterface.setCatid(yapiMenu.getId());
                    MethodPersistenceInfo methodPersistence = method.getPersistence();
                    Map<String, String> desc = method.getDesc();
                    if (StrUtil.isNotBlank(methodPersistence.getRename())) {
                        yapiInterface.setDesc(methodPersistence.getRename());
                    } else {
                        String methodDesc = DescProcessUtil.getDesc(descSorts, desc);
                        yapiInterface.setDesc(StrUtil.isNotBlank(methodDesc) ? methodDesc : method.getMethodName());
                    }
                    yapiInterface.setMethod(requestMethod);

                });

            });
        });

    }

    private void refreshProject(YapiSetting setting) {
        projectBox.removeAllItems();
        YapiGroup selectedItem = (YapiGroup) groupBox.getSelectedItem();
        if (selectedItem != null) {
            List<YapiProject> projects = YapiRequestUtil.getProjects(setting, selectedItem.getId());
            if (CollUtil.isNotEmpty(projects)) {
                projects.forEach(projectBox::addItem);
            }
        }
    }
}
